package com.huoxingy.print;

import com.huoxingy.print.domain.OrderPrintTask;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * Created by hanxi on 2017/12/25.
 */
@Component
@Slf4j
public class PrintTaskJsonParser {
    private Configuration jsonConfig;

    private DateFormatter df;

    public PrintTaskJsonParser() {
        df = new DateFormatter();
        df.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        df.setPattern("yyyy-MM-dd HH:mm:ss");
    }

    List<OrderPrintTask> parse(String json) {
        List<OrderPrintTask> prints = new ArrayList<>();

        Object document = jsonConfig.jsonProvider().parse(json);

        String creatorName = JsonPath.read(document, "$.creatorName");
        String tableNo;
        try {
            tableNo = JsonPath.read(document, "$.tableNo");
        } catch (PathNotFoundException e) {
            tableNo = null;
        }
        String orderCode = JsonPath.read(document, "$.code");
        long createTimestamp = JsonPath.read(document, "$.createTime");
        String notes;
        try {
            notes = JsonPath.read(document, "$.notes");
        } catch (PathNotFoundException e) {
            notes = null;
        }
        String storeId = JsonPath.read(document, "$.storeId");

        long updateTimeStamp = JsonPath.read(document, "$.updateTime");

        List<Map<String, Object>> items = JsonPath.read(document, "$.items[?(@.kitchenPrintEnabled == true)]");
        for (Map<String, Object> item : items) {
            String productName = item.get("productName").toString();

            OrderPrintTask task = new OrderPrintTask();
            task.setCreatorName(creatorName);
            task.setNotes(notes);
            task.setOrderCode(orderCode);
            task.setProductName(productName);
            task.setStoreId(storeId);
            task.setTableNo(tableNo);
            task.setQuantity(Short.parseShort(item.get("quantity").toString()));
            task.setPrice(item.get("salesPrice").toString());

            task.setCreateTime(df.print(new Date(createTimestamp), Locale.CHINA));

            Date now = new Date();
            task.setPrintTime(df.print(now, Locale.CHINA));
            task.setProcessTime(df.print(new Date(updateTimeStamp), Locale.CHINA));
            if ((now.getTime() - updateTimeStamp) > 300000) {
                log.warn("Print task:{} is overdue, print time:{}, process time:{}", task.getOrderCode(), task.getPrintTime(), task.getProcessTime());
            }

            prints.add(task);
        }

        return prints;
    }

    @Autowired
    public void setJsonConfig(Configuration jsonConfig) {
        this.jsonConfig = jsonConfig;
    }
}
